<?php
error_reporting(E_ERROR);
$con = mysql_connect("localhost:3306","root","111111");
if (!$con)
{
	die('Could not connect: ' . mysql_error());
}
echo "connect successful";
mysql_select_db("heavendb", $con);
$sql = "update user set user_name = 'heaven' where user_name = 'haiwei'";
$sql_two = "INSERT INTO `user` (`user_name`) VALUES ('sunday');";
$sql_three = "select * from user";
/*
 * 查询，修改，增加，删除数据库数据的函数
 * Q:$row['FirstName'] 和  $row['LastName'] 为何没有用
 * A:
 */
//mysql_query($sql,$con);
//mysql_query($sql_two,$con);
$result = mysql_query($sql_three,$con);
while($row = mysql_fetch_array($result,MYSQL_ASSOC))
  {
    $data[]=$row;
  }
  
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
$data=array();
foreach( $data as $row)
  {
      $data[]=$row;
      echo "<tr>";
	  echo "<td>" . $row['user_id'] . "</td>";
	  echo "<td>" . $row['user_name'] . "</td>";
	  echo "</tr>";
  }
echo "</table>";
echo "<br>准备关闭数据库...";
mysql_close($con);
echo "<br>数据库已关闭!";
?>


<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>" method="post">
id: <input type="text" name="userid" /><br>
name: <input type="text" name="username" /><br>
<input type="submit" />
</form>


<?php 
$userid = $username = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
	$filters = array(
		"userid" => FILTER_VALIDATE_INT,
		"username" => FILTER_SANITIZE_STRING
	);
	$result = filter_input_array(INPUT_POST,$filters);
	if (!$result["userid"]) {
		echo "请输入数字";
		return ;
	}
	if (!$result["username"]) {
		echo "请输入文字";
		return ;
	}
	$userid = text_input($result["userid"]);
	$username = text_input($result["username"]);
	echo "userid={$userid}<br>username={$username}<br>";
}
function text_input($data){
	$data = trim($data);
	$data = stripcslashes($data);
	$data = htmlspecialchars($data);
	return $data;
}
//操作数据库
$con = mysql_connect("localhost:3306","root","111111");
if (!$con) {
	die('无法连接数据库:'.mysql_error());
}
echo "connect success !";
mysql_select_db("heavendb",$con);
echo "<br>insert into user (user_id,user_name) values('{$userid}','{$username}')";
$sql = "insert into user (user_id,user_name) values('{$userid}','{$username}')";
mysql_query($sql,$con);
echo "<br>准备关闭数据库...";
mysql_close($con);
echo "<br>数据库已关闭!";
?>




